
--[[
	***********************
	* MCServer - thomasfn *
	***********************
	util.lua - Loads utility functions
--]]

function table.Merge( dst, src )
	for key, val in pairs( src ) do
		dst[ key ] = val
	end
end

function table.FromString( str, seperator )
	local pos, arr = 0, {}
	for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
		table.insert( arr, string.sub( str, pos, st-1 ) ) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	table.insert( arr, string.sub( str, pos ) ) -- Attach chars right of last divider
	return arr
end

function math.Clamp( val, min, max )
	return ((val < min) and min) or ((val > max) and max) or val
end

util = {}

function util.BroadcastChat( tag, msg )
	local str = ""
	if (tag) then
		if (tag == "") then tag = SERVER_TAG end
		str = SERVER_TAG_COL .. "[" .. tag .. "] " .. SERVER_MSG_COL .. msg
	else
		str = SERVER_MSG_COL .. msg
	end
	core.EntityManager:BroadcastChat( str )
end

function util.BroadcastChat( msg )
	core.EntityManager:BroadcastChat( msg )
end

function util.FindPlayers( name )
	local plist = core.EntityManager:GetAllPlayers()
	local tmp = {}
	for i=1, plist.Count do
		local pl = plist[i-1]
		local nick = pl:Nick()
		if (string.find( nick, name )) then table.insert( tmp, pl ) end
	end
	return tmp
end